package com.yhn.init.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.init.common.DeleteRequest;
import com.yhn.init.common.ErrorCode;
import com.yhn.init.exception.BusinessException;
import com.yhn.init.exception.ThrowUtils;
import com.yhn.init.mapper.MenuMapper;
import com.yhn.init.model.dto.menu.MenuAddRequest;
import com.yhn.init.model.dto.menu.MenuQueryRequest;
import com.yhn.init.model.dto.menu.MenuUpdateRequest;
import com.yhn.init.model.entity.Menu;
import com.yhn.init.model.vo.MenuVO;
import com.yhn.init.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public Page<MenuVO> queryPage(MenuQueryRequest menuQueryRequest) {
        if (menuQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = menuQueryRequest.getCurrent();
        long size = menuQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Menu> menuPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(menuQueryRequest));
        Page<MenuVO> menuVOPage = new Page<>(current, size, menuPage.getTotal());
        List<MenuVO> menuVO = this.getMenuVO(menuPage.getRecords());
        menuVOPage.setRecords(menuVO);
        return menuVOPage;
    }


    @Override
    public QueryWrapper<Menu> getQueryWrapper(MenuQueryRequest menuQueryRequest) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        // 如果请求对象为空，直接返回一个空的查询包装器
        if (menuQueryRequest == null) {
            return queryWrapper;
        }
        // 从请求对象中获取各项查询条件
        Long id = menuQueryRequest.getId();
        queryWrapper.eq(id != null && id > 0, "id", id);
        return queryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param menuList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<MenuVO> getMenuVO(List<Menu> menuList) {
        if (CollUtil.isEmpty(menuList)) {
            return new ArrayList<>();
        }
        return menuList.stream().map(this::getMenuVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param menu 原对象
     * @return 脱敏对象
     */
    @Override
    public MenuVO getMenuVO(Menu menu) {
        if (menu == null) {
            return null;
        }
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(menu, menuVO);
        return menuVO;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public MenuVO getInfoById(Long id) {
        Menu menu = this.getById(id);
        if (menu == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getMenuVO(menu);
    }

    /**
     * 保存信息
     *
     * @param menuAddRequest 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveMenu(MenuAddRequest menuAddRequest) {
        if (menuAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuAddRequest, menu);
        boolean result = this.save(menu);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return menu.getId();
    }


    /**
     * 修改信息
     *
     * @param menuUpdateRequest 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateMenu(MenuUpdateRequest menuUpdateRequest) {
        if (menuUpdateRequest == null || menuUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuUpdateRequest, menu);
        long id = menuUpdateRequest.getId();
        // 判断是否存在
        Menu oldMenu = this.getById(id);
        ThrowUtils.throwIf(oldMenu == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(menu);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, ErrorCode.NOT_FOUND_ERROR);
        return b;
    }
}
